<?php

include 'bur_controller.php';

class Payment_plan extends Bur_controller {
	var $detail_controller = 'bur/detail_payment_plan';
		
	function _view_content($bulan = NULL, $semester = NULL) {			
		if ($this->_is_student()) {
			redirect($this->detail_controller.'/view');
		} else {
			$students = $this->_students_id();
			
			if (is_null_or_false($bulan)) {
				$bulan = $this->_current_month();
			}
			
			if (is_null_or_false($semester)) {
				$semester = $this->_current_semester();
			}
			
			//XXX: find a better way to do this
			$this->db->join('(SELECT * FROM `bur_h_payment_plan` WHERE `key_aka_semester` = '.$semester.' AND `month` = '.$bulan.') AS payment_plan', 'aka_m_student.key = payment_plan.key_aka_student', 'left outer');
			$this->db->join('(SELECT * FROM `bur_h_charge` WHERE `key_aka_semester` = '.$semester.' AND `month` = '.$bulan.') AS charge', 'aka_m_student.key = charge.key_aka_student', 'left outer');
			$this->db->select('aka_m_student.key AS nim, name AS nama, key_aka_peer_year AS angkatan, charge.minimum_payment AS minimum, payment_plan.total, payment_plan.key_bur_payment_plan_status AS status');
			$data = $this->db->get('aka_m_student')->result_array();		
			
			$this->db->select('nkey, description');
			$_semesters = $this->db->get('aka_r_semester')->result_array();
			
			$semesters = array();
			foreach ($_semesters as $_idx => $_val) {
				$semesters[$_val['key']] = $_val['description'];
			}
			
			$this->db->select('key');
			$_peer_years = $this->db->get('aka_r_peer_year')->result_array();
			
			$peer_years = array();
			foreach ($_peer_years as $_idx => $_val) {
				$peer_years[] = $_val['key'];
			}
			
			$this->db->select('key');
			$_statuses = $this->db->get('bur_r_payment_plan_status')->result_array();
			
			$statuses = array();
			foreach ($_statuses as $_idx => $_val) {
				$statuses[] = $_val['key'];
			}
			
			$content_data = array(
				'controller' => get_unit_controller($this),
				'detail_controller' => $this->detail_controller,
				'semesters' => $semesters,
				'semester' => $semester,
				'semester_desc' => $this->_semester_desc($semester),
				'bulan' => $bulan,
				'peer_years' => $peer_years,
				'statuses' => $statuses,
				'data' => $data,				
			);
			$content = $this->load->view('bur/viewer/payment_plan', $content_data, TRUE);
		}
		return $content;
	}
	
	
	function index()
	{
		$controller = get_unit_controller($this);
		$this->common->flash_message($this->session->flashdata('message'), $this->session->flashdata('message_level'));		
		if ($this->_is_student()) { // current role = mahasiswa
			redirect($controller.'/add');
		} else {
			redirect($controller.'/view');
		}
	}
	
	function view($bulan = NULL, $semester = NULL)
	{
		$data = array(
			'application' => get_application($this),
			'application_title' => $this->common->get_application_description(get_application($this)),
			'unit' => get_unit($this),
			'unit_title' => $this->common->get_unit_description(get_unit($this)),
			'unit_controller' => get_unit_controller($this),
			'page' => 'View Data',
			'navigation' => $this->_navigation(),
			'content' => $this->_view_content($bulan, $semester),
		);
	
		$this->load->view('layout/default', $data);		
	}
		
	function add($nim = NULL)
	{		
		redirect($this->detail_controller.'/add');
	}

	function process_add()
	{

	}
	
	function update($nim = NULL)
	{							
		// untuk update status
	}
	
	function process_update()
	{		

	}
}
